package org.mcb.zebra.portal.manager;

import javax.portlet.PortalContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.pluto.PortletWindow;
import org.apache.pluto.PortletWindowID;
import org.apache.pluto.spi.PortalCallbackService;
import org.apache.pluto.spi.PortletURLProvider;
import org.apache.pluto.spi.ResourceURLProvider;
import org.mcb.zebra.portal.page.PageManager;
import org.mcb.zebra.portal.resource.ResourceConfig;
import org.mcb.zebra.portal.services.ZebPortletURLProvider;
import org.mcb.zebra.portal.services.ZebResourceURLProvider;
import org.mcb.zebra.portal.url.UrlParser;
import org.mcb.zebra.portal.window.ZebPortletWindow;
import org.mcb.zebra.portal.window.ZebPortletWindowID;

public class ZebPortalManager implements PortalManager {
	private static PortalManager instance = null;

	public static synchronized PortalManager getInstance() {
		if (instance == null)
			instance = new ZebPortalManager();
		return instance;
	}

	private PortalContext portalContext;
	private ResourceConfig config;
	private PortalCallbackService callbackService;
	private UrlParser urlParser;

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.mcb.zebra.portal.manager.PortalManager#getUrlParser()
	 */
	public UrlParser getUrlParser() {
		return urlParser;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.mcb.zebra.portal.manager.PortalManager#setUrlParser(org.mcb.zebra.portal.url.UrlParser)
	 */
	public void setUrlParser(UrlParser urlParser) {
		this.urlParser = urlParser;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.mcb.zebra.portal.manager.PortalManager#getCallbackService()
	 */
	public PortalCallbackService getCallbackService() {
		return callbackService;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.mcb.zebra.portal.manager.PortalManager#setCallbackService(org.apache.pluto.spi.PortalCallbackService)
	 */
	public void setCallbackService(PortalCallbackService callbackService) {
		this.callbackService = callbackService;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.mcb.zebra.portal.manager.PortalManager#getPortalContext()
	 */
	public PortalContext getPortalContext() {
		return portalContext;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.mcb.zebra.portal.manager.PortalManager#setPortalContext(javax.portlet.PortalContext)
	 */
	public void setPortalContext(PortalContext portalContext) {
		this.portalContext = portalContext;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.mcb.zebra.portal.manager.PortalManager#getConfig()
	 */
	public ResourceConfig getConfig() {
		return config;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.mcb.zebra.portal.manager.PortalManager#setConfig(org.mcb.zebra.portal.resource.ResourceConfig)
	 */
	public void setConfig(ResourceConfig config) {
		this.config = config;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.mcb.zebra.portal.manager.PortalManager#getPortletURLProvider(javax.servlet.http.HttpServletRequest,
	 *      org.apache.pluto.PortletWindow)
	 */
	public PortletURLProvider getPortletURLProvider(HttpServletRequest request,
			PortletWindow portletWindow) {
		return new ZebPortletURLProvider(request, portletWindow);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.mcb.zebra.portal.manager.PortalManager#getResourceURLProvider(javax.servlet.http.HttpServletRequest,
	 *      org.apache.pluto.PortletWindow)
	 */
	public ResourceURLProvider getResourceURLProvider(
			HttpServletRequest request, PortletWindow portletWindow) {
		return new ZebResourceURLProvider(request, portletWindow);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.mcb.zebra.portal.manager.PortalManager#getPageManager()
	 */
	public PageManager getPageManager() {
		return config != null ? config.getPageManager() : null;
	}
	/*
	 * (non-Javadoc)
	 * @see org.mcb.zebra.portal.manager.PortalManager#getDefaultPage()
	 */
	public String getDefaultPage() {
		return config.getPageManager().getDefaultPage();
	}
	/*
	 * (non-Javadoc)
	 * @see org.mcb.zebra.portal.manager.PortalManager#setDefaultPage(java.lang.String)
	 */
	public void setDefaultPage(String defaultPage) {
		config.setDefaultPage(defaultPage);
	}
	/*
	 * (non-Javadoc)
	 * @see org.mcb.zebra.portal.manager.PortalManager#getPortletWindow(java.lang.String, java.lang.String)
	 */
	@Override
	public PortletWindow getPortletWindow(String context, String portletName) {
		PortletWindowID windowID  = ZebPortletWindowID.getId(context, portletName);
		PortletWindow portletWindow = ZebPortletWindow.getPortletWindow(context, portletName, windowID);
		return portletWindow;
	}

}
